package edu.scau.mis.web.controller;

import edu.scau.mis.cloth.entity.GsAttrs;
import edu.scau.mis.cloth.service.GsAttrsService;
import edu.scau.mis.cloth.service.GsClothesService;
import edu.scau.mis.cloth.service.GsDiscountService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

/**
 * (GsAttrs)表控制层
 *
 * @author CJW
 * @since 2022-04-02 20:28:04
 */
@RestController
@RequestMapping("gsDiscount")
public class GsDiscountController {

    @Autowired
    private GsDiscountService gsDiscountService;
    @Autowired
    private GsClothesService gsClothesService;
//  分页显示优惠表的商品id 名称 优惠金额
    @GetMapping("/queryAll")
    public PageInfo queryAll(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize){
       PageHelper.startPage(pageNum,pageSize);
       List list=gsDiscountService.queryAll();
       PageInfo pageInfo=new PageInfo(list);
       return pageInfo;
    }
//    修改商品优惠
    @GetMapping("changeDiscount/{clId}/{discount}")
    public int changeDiscount(@PathVariable("clId")Integer clId,@PathVariable("discount")Integer discount){
        return gsDiscountService.changeDiscount(clId, discount);
    }
//    删除商品优惠
    @GetMapping("deleteDiscount/{clId}")
    public int deleteDiscount(@PathVariable("clId")Integer clId){
        return gsDiscountService.deleteDiscount(clId);
    }

//    添加商品优惠 注意判断cl_id是否存在，不存在返回FALSE 存在返回TRUE,根据clId查询gs_clothes,看是否有记录
    @GetMapping("insertDiscount/{clId}/{discount}")
    public boolean insertDiscount(@PathVariable("clId")Integer clId,@PathVariable("discount")Integer discount){
        if(gsClothesService.queryId(clId)==null){
//            System.out.println(gsClothesService.queryId(clId));
            return FALSE;
        }
        else {
            //判断cl_id是否已经存在优惠，存在则进行修改否则插入
            if(gsDiscountService.queryById(clId)==null){
                gsDiscountService.insertDiscount(clId, discount);
            }
            else gsDiscountService.changeDiscount(clId,discount);
            return TRUE;
        }
    }
}



